package com.wthink.newMain.settings.menuManager.inner

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.wthink.card.models.GMBInfoBundle
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentMenuManagerLineBinding
import com.wthink.hzgj.util.PreferencesUtils
import com.wthink.hzgj.util.adapter.BaseViewHolder
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.newMain.settings.menuManager.inner.viewmodel.LineChangeAdapter
import com.wthink.newMain.settings.menuManager.inner.viewmodel.MenuManagerLineViewmodel
import com.wthink.newMain.view.changeLine.ChangeLineDialogAdapter
import kotlinx.android.synthetic.main.item_change_line_dialog.view.*

class MenuManagerLineFragment : Fragment(){
    private val viewModel by viewModels<MenuManagerLineViewmodel>()

    private lateinit var binding: FragmentMenuManagerLineBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_menu_manager_line,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.start()

        binding.iv1.isSelected = true
        binding.iv2.isSelected = true

        binding.iv1.setOnClickListener {
            binding.iv1.isSelected = true
            binding.iv4.isSelected = false
        }
        binding.iv2.setOnClickListener {
            binding.iv2.isSelected = true
            binding.iv3.isSelected = false
        }
        binding.iv3.setOnClickListener {
            binding.iv3.isSelected = true
            binding.iv2.isSelected = false
        }
        binding.iv4.setOnClickListener {
            binding.iv1.isSelected = false
            binding.iv4.isSelected = true
        }

        var lastPosition = PreferencesUtils.getInt("linePosition")

        binding.dialogLineRecycle.layoutManager = GridLayoutManager(context, 3)
        binding.dialogLineRecycle.adapter = object : LineChangeAdapter() {
            override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
                super.onBindViewHolder(holder, position)
                holder.itemView.iv1.isSelected = position == lastPosition
                if (holder.itemView.iv1.isSelected) {
                    //lineNum = holder.itemView.tv1.text.toString()
                    //lineId = list!![position].id
                }

                holder.itemView.setOnClickListener {
                    val position1 = lastPosition
                    //GMBInfoBundle.setLastPosition(position)
                    lastPosition = position
                    notifyItemChanged(position)
                    notifyItemChanged(position1)
                    PreferencesUtils.saveValue("linePosition",position)
                    PreferencesUtils.saveValue("lineId",viewModel.lineList.value!!.list[position].id)
                    // lineNum = holder.itemView.tv1.text.toString()
                }
            }
        }.apply {
            /*   if (list != null) {
                     update(list)
                 }*/
            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    //chosen?.isSelected = true
                }
            }
        }
    }
}